home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
bipl.zip
/
PROCS.ZIP
/
PERMUTE.ICN
< prev
next >
Wrap
Text File
|
1992-09-28
|
2KB
|
95 lines
############################################################################
#
# File: permute.icn
#
# Subject: Procedures for permutations, combinations, and such
#
# Author: Ralph E. Griswold and Kurt A. Welgehausen
#
# Date: September 2, 1991
#
###########################################################################
#
# These procedures produce various rearrangements of strings of
# characters:
#
# comb(s,i) generates the combinations characters from s taken
# i at a time.
#
# permute(s) generates all the permutations of the string s.
#
# menader(s,n) produces a "meandering" string which contains all
# n-tuples of characters of s.
#
# csort(s) produces the characters of s in lexical order.
#
# ochars(s) produces the unique characters of s in the order they
# first appear in s.
#
# schars(s) produces the unique characters of s in lexical order.
#
############################################################################
procedure comb(s,i)
local j
if i < 1 then fail
suspend if i = 1 then !s
else s[j := 1 to *s - i + 1] || comb(s[j + 1:0],i - 1)
end
procedure permute(s)
local i
if *s = 0 then return ""
suspend s[i := 1 to *s] || permute(s[1:i] || s[i+1:0])
end
procedure meander(alpha,n)
local result, trial, t, i, c
i := *alpha
t := n - 1
result := repl(alpha[1],t) # base string
while c := alpha[i] do { # try a character
result ? { # get the potential n-tuple
tab(-t)
trial := tab(0) || c
}
if result ? find(trial) then # duplicate, work back
i -:= 1
else {
result ||:= c # add it
i := *alpha # and start from end again
}
}
return result
end
procedure csort(s)
local c, s1
s1 := ""
every c := !cset(s) do
every find(c,s) do
s1 ||:= c
return s1
end
procedure schars(s)
return string(cset(s))
end
procedure ochars(w)
local out, c
out := ""
every c := !w do
if not find(c,out) then
out ||:= c
return out
end